<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.5"/>
<title>MCUXpresso SDK API Reference Manual: Semihosting</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
  $(window).load(resizeHeight);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { searchBox.OnSelectItem(0); });
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="customdoxygen.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="fs_logo.gif"/></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">MCUXpresso SDK API Reference Manual
   &#160;<span id="projectnumber">Rev. 0</span>
   </div>
   <div id="projectbrief">NXP Semiconductors</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.5 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="pages.html"><span>Related&#160;Pages</span></a></li>
      <li><a href="modules.html"><span>API&#160;Reference</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('group__Semihosting.html','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Data Structures</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&#160;</span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&#160;</span>Enumerator</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark">&#160;</span>Groups</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(8)"><span class="SelectionMark">&#160;</span>Pages</a></div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">Semihosting<div class="ingroups"><a class="el" href="group__debugconsole.html">Debug Console</a></div></div>  </div>
</div><!--header-->
<div class="contents">
<p>Semihosting is a mechanism for ARM targets to communicate input/output requests from application code to a host computer running a debugger. This mechanism can be used, for example, to enable functions in the C library, such as printf() and scanf(), to use the screen and keyboard of the host rather than having a screen and keyboard on the target system.</p>
<h1><a class="anchor" id="SemihostingGrps"></a>
Guide Semihosting for IAR</h1>
<p><b>NOTE:</b> After the setting both "printf" and "scanf" are available for debugging.</p>
<h2>Step 1: Setting up the environment</h2>
<ol type="1">
<li>To set debugger options, choose Project&gt;Options. In the Debugger category, click the Setup tab.</li>
<li>Select Run to main and click OK. This ensures that the debug session starts by running the main function.</li>
<li>The project is now ready to be built.</li>
</ol>
<h2>Step 2: Building the project</h2>
<ol type="1">
<li>Compile and link the project by choosing Project&gt;Make or F7.</li>
<li>Alternatively, click the Make button on the tool bar. The Make command compiles and links those files that have been modified.</li>
</ol>
<h2>Step 3: Starting semihosting</h2>
<ol type="1">
<li>Choose "Semihosting_IAR" project -&gt; "Options" -&gt; "Debugger" -&gt; "J-Link/J-Trace".</li>
<li>Choose tab "J-Link/J-Trace" -&gt; "Connection" tab -&gt; "SWD".</li>
<li>Start the project by choosing Project&gt;Download and Debug.</li>
<li>Choose View&gt;Terminal I/O to display the output from the I/O operations.</li>
</ol>
<h1><a class="anchor" id="SemihostingKeilGrps"></a>
Guide Semihosting for Keil µVision</h1>
<p><b>NOTE:</b> Keil supports Semihosting only for Cortex-M3/Cortex-M4 cores.</p>
<h2>Step 1: Prepare code</h2>
<p>Remove function fputc and fgetc is used to support KEIL in "fsl_debug_console.c" and add the following code to project.</p>
<div class="fragment"><div class="line"><span class="preprocessor">#pragma import(__use_no_semihosting_swi)</span></div>
<div class="line"><span class="preprocessor"></span></div>
<div class="line"><span class="keyword">volatile</span> <span class="keywordtype">int</span> ITM_RxBuffer = ITM_RXBUFFER_EMPTY;       <span class="comment">/* used for Debug Input */</span></div>
<div class="line"><span class="keyword">struct </span>__FILE</div>
<div class="line">{</div>
<div class="line">    <span class="keywordtype">int</span> handle;</div>
<div class="line">};</div>
<div class="line">FILE __stdout;</div>
<div class="line">FILE __stdin;</div>
<div class="line"></div>
<div class="line"><span class="keywordtype">int</span> fputc(<span class="keywordtype">int</span> ch, FILE *f)</div>
<div class="line">{</div>
<div class="line">    <span class="keywordflow">return</span> (ITM_SendChar(ch));</div>
<div class="line">}</div>
<div class="line"></div>
<div class="line"><span class="keywordtype">int</span> fgetc(FILE *f)</div>
<div class="line">{ <span class="comment">/* blocking */</span></div>
<div class="line">    <span class="keywordflow">while</span> (ITM_CheckChar() != 1)</div>
<div class="line">        ;</div>
<div class="line">    <span class="keywordflow">return</span> (ITM_ReceiveChar());</div>
<div class="line">}</div>
<div class="line"></div>
<div class="line"><span class="keywordtype">int</span> ferror(FILE *f)</div>
<div class="line">{</div>
<div class="line">    <span class="comment">/* Your implementation of ferror */</span></div>
<div class="line">    <span class="keywordflow">return</span> EOF;</div>
<div class="line">}</div>
<div class="line"></div>
<div class="line"><span class="keywordtype">void</span> _ttywrch(<span class="keywordtype">int</span> ch)</div>
<div class="line">{</div>
<div class="line">    ITM_SendChar(ch);</div>
<div class="line">}</div>
<div class="line"></div>
<div class="line"><span class="keywordtype">void</span> _sys_exit(<span class="keywordtype">int</span> return_code)</div>
<div class="line">{</div>
<div class="line">label:</div>
<div class="line">    <span class="keywordflow">goto</span> label; <span class="comment">/* endless loop */</span></div>
<div class="line">}</div>
</div><!-- fragment --><h2>Step 2: Setting up the environment</h2>
<ol type="1">
<li>In menu bar, choose Project&gt;Options for target or using Alt+F7 or click.</li>
<li>Select "Target" tab and not select "Use MicroLIB".</li>
<li>Select “Debug” tab, select “J-Link/J-Trace Cortex” and click “Setting button”.</li>
<li>Select “Debug” tab and choose Port:SW, then select "Trace" tab, choose "Enable" and click OK.</li>
</ol>
<h2>Step 3: Building the project</h2>
<ol type="1">
<li>Compile and link the project by choosing Project&gt;Build Target or using F7.</li>
</ol>
<h2>Step 4: Building the project</h2>
<ol type="1">
<li>Choose “Debug” on menu bar or Ctrl F5.</li>
<li>In menu bar, choose "Serial Window" and click to "Debug (printf) Viewer".</li>
<li>Run line by line to see result in Console Window.</li>
</ol>
<h1><a class="anchor" id="SemihostingGrps1"></a>
Guide Semihosting for KDS</h1>
<p><b>NOTE:</b> After the setting use "printf" for debugging.</p>
<h2>Step 1: Setting up the environment</h2>
<ol type="1">
<li>In menu bar, choose Project&gt;Properties&gt;C/C++ Build&gt;Settings&gt;Tool Settings.</li>
<li>Select “Libraries” on “Cross ARM C Linker” and delete “nosys”.</li>
<li>Select “Miscellaneous” on "Cross ARM C Linker”, add “-specs=rdimon.specs” to “Other link flages” and tick “Use newlib-nano”, and click OK.</li>
</ol>
<h2>Step 2: Building the project</h2>
<ol type="1">
<li>In menu bar, choose Project&gt;Build Project.</li>
</ol>
<h2>Step 3: Starting semihosting</h2>
<ol type="1">
<li>In Debug configurations, choose "Startup" tab, tick “Enable semihosting and Telnet”. Press “Apply” and “Debug”.</li>
<li>After clicking Debug, the Window is displayed same as below. Run line by line to see the result in the Console Window.</li>
</ol>
<h1><a class="anchor" id="SemihostingGrps9"></a>
Guide Semihosting for ATL</h1>
<p><b>NOTE:</b> J-Link has to be used to enable semihosting.</p>
<h2>Step 1: Prepare code</h2>
<p>Add the following code to the project.</p>
<div class="fragment"><div class="line"><span class="keywordtype">int</span> _write(<span class="keywordtype">int</span> file, <span class="keywordtype">char</span> *ptr, <span class="keywordtype">int</span> len)</div>
<div class="line">{</div>
<div class="line">  <span class="comment">/* Implement your write code here. This is used by puts and printf. */</span></div>
<div class="line">  <span class="keywordtype">int</span> i=0;</div>
<div class="line">  <span class="keywordflow">for</span>(i=0 ; i&lt;len ; i++)</div>
<div class="line">    ITM_SendChar((*ptr++));</div>
<div class="line">  <span class="keywordflow">return</span> len;</div>
<div class="line">}</div>
</div><!-- fragment --><h2>Step 2: Setting up the environment</h2>
<ol type="1">
<li>In menu bar, choose Debug Configurations. In tab "Embedded C/C++ Aplication" choose "Semihosting_ATL_xxx debug J-Link".</li>
<li>In tab "Debugger" set up as follows.<ul>
<li>JTAG mode must be selected</li>
<li>SWV tracing must be enabled</li>
<li>Enter the Core Clock frequency, which is hardware board-specific.</li>
<li>Enter the desired SWO Clock frequency. The latter depends on the JTAG Probe and must be a multiple of the Core Clock value.</li>
</ul>
</li>
<li>Click "Apply" and "Debug".</li>
</ol>
<h2>Step 3: Starting semihosting</h2>
<ol type="1">
<li>In the Views menu, expand the submenu SWV and open the docking view “SWV Console".
2.  Open the SWV settings panel by clicking the "Configure Serial Wire Viewer" button in the SWV Console view toolbar.
3.  Configure the data ports to be traced by enabling the ITM channel 0 check-box in the ITM stimulus ports group: Choose "EXETRC: Trace Exceptions" and In tab "ITM Stimulus Ports" choose 
"Enable Port" 0. Then click "OK".</li>
<li>It is recommended not to enable other SWV trace functionalities at the same time because this may over use the SWO pin causing packet loss due to a limited bandwidth (certain other SWV tracing capabilities can send a lot of data at very high-speed). Save the SWV configuration by clicking the OK button. The configuration is saved with other debug configurations and remains effective until changed.</li>
<li>Press the red Start/Stop Trace button to send the SWV configuration to the target board to enable SWV trace recoding. The board does not send any SWV packages until it is properly configured. The SWV Configuration must be present, if the configuration registers on the target board are reset. Also, tracing does not start until the target starts to execute.</li>
<li>Start the target execution again by pressing the green Resume Debug button.</li>
<li>The SWV console now shows the printf() output.</li>
</ol>
<h1><a class="anchor" id="SemihostingGrps11"></a>
Guide Semihosting for ARMGCC</h1>
<h2>Step 1: Setting up the environment</h2>
<ol type="1">
<li>Turn on "J-LINK GDB Server" -&gt; Select suitable "Target device" -&gt; "OK".</li>
<li>Turn on "PuTTY". Set up as follows.<ul>
<li>"Host Name (or IP address)" : localhost</li>
<li>"Port" :2333</li>
<li>"Connection type" : Telet.</li>
<li>Click "Open".</li>
</ul>
</li>
<li>Increase "Heap/Stack" for GCC to 0x2000: </li>
</ol>
<p><b> Add to "CMakeLists.txt" </b></p>
<p>SET(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} --defsym=__stack_size__=0x2000")</p>
<p>SET(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} --defsym=__stack_size__=0x2000")</p>
<p>SET(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} --defsym=__heap_size__=0x2000")</p>
<p>SET(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} --defsym=__heap_size__=0x2000") </p>
<h2>Step 2: Building the project</h2>
<ol>
<li>
<p class="startli">Change "CMakeLists.txt": </p>
<p><b> Change </b> "SET(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} &ndash;specs=nano.specs")" </p>
<p><b>to </b> "SET(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} &ndash;specs=rdimon.specs")"</p>
<p></p>
<p><b> Replace paragraph </b> </p>
<p>SET(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -fno-common")</p>
<p>SET(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -ffunction-sections")</p>
<p>SET(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -fdata-sections")</p>
<p>SET(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -ffreestanding")</p>
<p>SET(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -fno-builtin")</p>
<p>SET(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -mthumb")</p>
<p>SET(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -mapcs")</p>
<p>SET(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -Xlinker")</p>
<p>SET(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} --gc-sections")</p>
<p>SET(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -Xlinker")</p>
<p>SET(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -static")</p>
<p>SET(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -Xlinker")</p>
<p>SET(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -z")</p>
<p>SET(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -Xlinker")</p>
<p>SET(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} muldefs")</p>
<p></p>
<p><b> To </b></p>
<p></p>
<p>SET(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} --specs=rdimon.specs ") </p>
<p></p>
<p></p>
<p><b> Remove </b> </p>
<p>target_link_libraries(semihosting_ARMGCC.elf debug nosys)</p>
<p class="endli"></p>
</li>
<li>
<p class="startli">Run "build_debug.bat" to build project</p>
<h2>Step 3: Starting semihosting</h2>
<ol type="1">
<li>Download the image and set as follows. <div class="fragment"><div class="line">cd D:\mcu-sdk-2.0-origin\boards\twrk64f120m\driver_examples\semihosting\armgcc\debug</div>
<div class="line">d:</div>
<div class="line">C:\PROGRA~2\GNUTOO~1\4BD65~1.920\bin\arm-none-eabi-gdb.exe</div>
<div class="line">target remote localhost:2331</div>
<div class="line">monitor reset</div>
<div class="line">monitor semihosting enable</div>
<div class="line">monitor semihosting thumbSWI 0xAB</div>
<div class="line">monitor semihosting IOClient 1</div>
<div class="line">monitor flash device = MK64FN1M0xxx12</div>
<div class="line">load semihosting_ARMGCC.elf</div>
<div class="line">monitor reg pc = (0x00000004)</div>
<div class="line">monitor reg sp = (0x00000000)</div>
<div class="line"><span class="keywordflow">continue</span></div>
</div><!-- fragment --></li>
<li>After the setting, press "enter". The PuTTY window now shows the printf() output. </li>
</ol>
</li>
</ol>
</div><!-- contents -->
</div><!-- doc-content -->
<!-- HTML footer for doxygen 1.8.5-->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul class="foot">
    <li class="footer">&copy; 2016 NXP Semiconductors. All rights reserved.
    </li>
  </ul>
</div>
</body>
</html>
